package com.zhangtai.modules.service.impl.certificate;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhangtai.modules.dao.CertificateDao;
import com.zhangtai.modules.entity.certificate.CertificateEntity;
import com.zhangtai.modules.service.certificate.CertificateService;
import com.zhangtai.utils.PageResult;
import com.zhangtai.utils.R;
import org.apache.commons.lang3.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;


@RestController
public class CertificateServiceImpl extends ServiceImpl<CertificateDao, CertificateEntity> implements CertificateService {

    @Override
    public PageResult queryPage(@RequestParam(value = "oid", required = false) String oid,
                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {

        IPage<CertificateEntity> page = this.page(
                new Page<CertificateEntity>(pageNum,pageSize),
                new QueryWrapper<CertificateEntity>().like(!StringUtils.isEmpty(oid),"oid",oid)
                        .eq("is_delete",0)
        );

        return new PageResult(page);
    }

    @Override
    public PageResult<CertificateEntity> getCertificateList(@RequestParam(value = "code", required = false) String code,
                                                 @RequestParam(value = "name", required = false) String name,
                                                 @RequestParam(value = "unitId", required = false) List<Long> unitId,
                                                 @RequestParam(value = "orgIds", required = false) List<Long> orgIds,
                                                 @RequestParam(value = "jobId", required = false) List<Long> jobId,
                                                 @RequestParam(value = "startDate", required = false) String startDate,
                                                 @RequestParam(value = "endDate", required = false) String endDate,
                                                 @RequestParam(value = "modelId", required = false) Long modelId,
                                                 @RequestParam(value = "status", required = false) List<Integer> status,
                                                 @RequestParam(value = "oid", required = false) Long oid,
                                                 @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                 @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize){
        IPage<CertificateEntity> page = this.page(
                new Page<CertificateEntity>(pageNum,pageSize),
                new QueryWrapper<CertificateEntity>().like(!StringUtils.isEmpty(name),"name",name)
                        .like(!StringUtils.isEmpty(code),"code",code)
                        .in(!CollectionUtils.isEmpty(unitId),"unit_id",unitId)
                        .in(!CollectionUtils.isEmpty(jobId),"job_id",jobId)
                        .between(!StringUtils.isEmpty(startDate)&&!StringUtils.isEmpty(endDate),"create_time",startDate,endDate)
                        .eq(!ObjectUtils.isEmpty(modelId),"model_id",modelId)
                        .in(!CollectionUtils.isEmpty(status),"status",status)
                        .eq(!ObjectUtils.isEmpty(oid),"oid",oid)
                        .and(ObjectUtils.isEmpty(oid),wra->wra.in(!CollectionUtils.isEmpty(orgIds),"unit_id",orgIds).or().
                                eq(CollectionUtils.isEmpty(orgIds),"unit_id",null))
                        .eq("is_delete",0)
                .orderByDesc("create_time")
        );

        return new PageResult<CertificateEntity>(page);
    }

    @Override
    public CertificateEntity getByIdE(Long id) {
            CertificateEntity one = this.getOne(new QueryWrapper<CertificateEntity>().eq("id", id).eq("is_delete", 0));

        return one;
    }

    @Override
    public List<CertificateEntity> getCertificateUnCallBack(){
        return this.list(new QueryWrapper<CertificateEntity>().eq("is_delete",0).eq("callback_status",0));
    }

    @Override
    public CertificateEntity getByEsignId(Integer id){
        CertificateEntity one = this.getOne(new QueryWrapper<CertificateEntity>().eq("flow_id", id).eq("is_delete", 0));
        return one;
    }

    @Override
    public R saveE(CertificateEntity entity) {
        boolean b = this.save(entity);
        if (!b){
            return R.error();
        }
        return R.ok();

    }

    @Override
    public R updateByIdE(CertificateEntity entity) {
        boolean b = this.updateById(entity);
        if (!b){
            return R.error();
        }
        return R.ok();
    }

    @Override
    public R removeOneById(Long id) {
            CertificateEntity entity = new CertificateEntity();
        entity.setId(id);
        entity.setIsDelete(1);
        boolean b = this.updateById(entity);
        if (!b){
            return R.error();
        }
        return R.ok();
    }

    @Override
    @Transactional
    public R removeByIdsE(List<Long> ids) {
        for (Long id : ids) {
            R r = removeOneById(id);
            if (!r.getFlag()){
                return r;
            }
        }
        return R.ok();
    }

}